package com.liulishuo.magicprogress.demo.ability;

import com.liulishuo.magicprogress.demo.ResourceTable;
import com.liulishuo.magicprogress.demo.util.Constant;
import com.liulishuo.magicprogress.demo.widget.AnimTextView;
import com.liulishuo.magicprogresswidget.Imag;
import com.liulishuo.magicprogresswidget.MagicProgressBar;
import com.liulishuo.magicprogresswidget.MagicProgressCircle;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * MainAbility
 *
 * @since 2012-10-15
 * Created by Jacksgong on 12/10/15.
 */
public class MainAbility extends Ability implements Component.ClickedListener {
    private Text textMainUrl;
    private MagicProgressCircle mbpMpc;
    private AnimTextView mbpTv;
    private MagicProgressBar mpbOne;
    private MagicProgressBar mpbTwo;
    private MagicProgressBar mpbThree;
    private MagicProgressBar mpbFour;
    private SecureRandom random;
    private float rdFloat = Constant.RD_MAX;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        initView();
        rdNum();
    }

    private void initView() {
        try {
            random = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            Logger.getLogger(MainAbility.class.getName()).log(Level.SEVERE, e.getMessage());
        }
        textMainUrl = (Text) findComponentById(ResourceTable.Id_text_main_url);
        mbpMpc = (MagicProgressCircle) findComponentById(ResourceTable.Id_mpb_mpc);
        mbpTv = (AnimTextView) findComponentById(ResourceTable.Id_mpb_tv);
        mpbOne = (MagicProgressBar) findComponentById(ResourceTable.Id_mpb_one);
        mpbTwo = (MagicProgressBar) findComponentById(ResourceTable.Id_mpb_two);
        mpbThree = (MagicProgressBar) findComponentById(ResourceTable.Id_mpb_three);
        mpbFour = (MagicProgressBar) findComponentById(ResourceTable.Id_mpb_four);
        findComponentById(ResourceTable.Id_re_random_percent).setClickedListener(this);
        textMainUrl.setClickedListener(this);
        findComponentById(ResourceTable.Id_increase_progress_smoothly).setClickedListener(this);
    }

    private void rdNum() {
        final int ceil = Constant.CEIL;
        final long duration = Constant.RD_DURATION;
        final int progress = random.nextInt(ceil);
        mbpMpc.setPercent(progress / rdFloat, duration);
        mbpTv.setPercent(progress / rdFloat, duration);
        mpbOne.setPercent(random.nextInt(ceil) / rdFloat, duration);
        mpbTwo.setPercent(random.nextInt(ceil) / rdFloat, duration);
        mpbThree.setPercent(random.nextInt(ceil) / rdFloat, duration);
        mpbFour.setPercent(random.nextInt(ceil) / rdFloat, duration);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_re_random_percent:
                rdNum();
                break;
            case ResourceTable.Id_increase_progress_smoothly:
                clickProgress();
                break;
            case ResourceTable.Id_text_main_url:
                toUrlPage();
                break;
            default:
                break;
        }
    }

    private void toUrlPage() {
        Intent intents = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withUri(Uri.parse(getString(ResourceTable.String_main_url_value)))
                .withAction(IntentConstants.ACTION_SEARCH)
                .build();
        intents.setOperation(operation);
        startAbility(intents);
    }

    private void clickProgress() {
        mbpMpc.setSmoothPercent(getIncreasedPercent(mbpMpc));
        mbpTv.setSmoothPercent(getIncreasedPercent(mbpMpc));
        mpbOne.setSmoothPercent(getIncreasedPercent(mpbOne), Constant.CLE_DURATION);
        mpbTwo.setSmoothPercent(getIncreasedPercent(mpbTwo));
        mpbThree.setSmoothPercent(getIncreasedPercent(mpbThree));
        mpbFour.setSmoothPercent(getIncreasedPercent(mpbFour));
    }

    /**
     * 随机值
     *
     * @param target imag
     * @return increasedPercent 最小值
     */
    private float getIncreasedPercent(Imag target) {
        float increasedPercent = Math.min(1, target.getPercent() + Constant.RD_ADD);
        return increasedPercent;
    }
}
